package main

import (
	"fmt"
	"net"
	"net/rpc"
	"net/rpc/jsonrpc"
	"time"
)

type AddRequest struct {
	Left  int
	Right int
}

type AddResponse struct {
	Result int
}

type Calc struct {
}

func (c *Calc) Add(req AddRequest, resp *AddResponse) error {
	fmt.Println("calc.add")
	time.Sleep(10 * time.Second)
	resp.Result = req.Left + req.Right
	return nil
}

func main() {
	rpc.Register(&Calc{})

	listener, _ := net.Listen("tcp", "0.0.0.0:8888")
	defer listener.Close()
	for {
		conn, err := listener.Accept()
		if err != nil {
			break
		}
		go jsonrpc.ServeConn(conn)
	}
}
